<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>

<div id="app">

    <!--表单-->
    <el-form :inline="true" :model="client_request" class="demo-form-inline">
        <el-form-item label="请求名称">
            <el-input v-model="client_request.name" placeholder="请求名称"></el-input>
        </el-form-item>
        <el-form-item label="请求类型">
            <el-select v-model="client_request.requestType" placeholder="请求类型">
                <el-option label="GET" value="1"></el-option>
                <el-option label="POST" value="2"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="请求体类型">
            <el-select v-model="client_request.bodyType" placeholder="请求体类型">
                <el-option label="FORM" value="1"></el-option>
                <el-option label="JSON" value="2"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>



    <!--按钮-->
    <el-row>
        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="dialogVisible = true">新增请求</el-button>
    </el-row>

    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑请求"
            :visible.sync="dialogVisible"
            width="30%">

        <el-form ref="form" :model="client_request" label-width="80px">
            <el-form-item label="请求名称">
                <el-input v-model="client_request.name"></el-input>
            </el-form-item>

            <el-form-item label="请求类型" prop="region">
                <el-select v-model="client_request.requestType" placeholder="请选择请求类型">
                    <el-option label="GET" value="1"></el-option>
                    <el-option label="POST" value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="请求地址">
                <el-input v-model="client_request.requestUrl"></el-input>
            </el-form-item>
            <el-form-item label="请求头">
                <el-input type="textarea" v-model="client_request.requestHeaders"></el-input>
            </el-form-item>
            <el-form-item label="请求体类型" prop="region">
                <el-select v-model="client_request.bodyType" placeholder="请选择请求体类型">
                    <el-option label="FORM" value="1"></el-option>
                    <el-option label="JSON" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="请求体">
                <el-input type="textarea" v-model="client_request.body"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addclientRequest">提交</el-button>
                <el-button @click="cancelAddclientRequest">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>



    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    label="序号"
                    align="center"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="name"
                    label="请求名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="requestTypeStr"
                    label="请求类型"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="requestUrl"
                    align="center"
                    label="请求地址">
            </el-table-column>
            <el-table-column
                    prop="requestHeaders"
                    align="center"
                    label="请求头">
            </el-table-column>
            <el-table-column
                    prop="bodyTypeStr"
                    align="center"
                    label="请求体类型">
            </el-table-column>
            <el-table-column
                    prop="body"
                    align="center"
                    label="请求体">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">
                    <el-button type="success">运行</el-button>
                    <el-button type="primary" @click="selectById(scope.row)">修改</el-button>
<!--                    <el-button type="danger" @click="deleteById(scope.row)">删除</el-button>-->
                </template>

            </el-table-column>
        </el-table>
    </template>

    <!--修改请求数据对话框表单-->
    <el-dialog
            title="修改请求数据"
            :visible.sync="dialogVisible2"
            width="30%">

        <el-form ref="form" :model="client_request" label-width="80px">
            <el-form-item label="请求名称">
                <el-input v-model="client_request.name"></el-input>
            </el-form-item>

            <el-form-item label="请求类型" prop="requestTypeStr">
                <el-select v-model="client_request.requestType" placeholder="请选择请求类型">
                    <el-option label="GET" value="1"></el-option>
                    <el-option label="POST" value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="请求地址">
                <el-input v-model="client_request.requestUrl"></el-input>
            </el-form-item>
            <el-form-item label="请求头">
                <el-input type="textarea" v-model="client_request.requestHeaders"></el-input>
            </el-form-item>
            <el-form-item label="请求体类型" prop="bodyTypeStr">
                <el-select v-model="client_request.bodyType" placeholder="请选择请求体类型">
                    <el-option label="FORM" value="1"></el-option>
                    <el-option label="JSON" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="请求体">
                <el-input type="textarea" v-model="client_request.body"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="updateClientRequest">提交</el-button>
                <el-button @click="cancelUpdateClientRequest">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>



    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>



<script src="js/axios-0.18.0.js"></script>

<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">


<script>
    new Vue({
        el:"#app",
        mounted(){
            // 当页面加载完成后执行的方法，发送异步请求，获取数据
            this.selectAll();
        },
        methods: {
            // 修改品牌数据时点击提交，更新数据的方法
            updateClientRequest() {
                var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/myProject-plus/clientRequest/update",
                    data: this.client_request
                }).then(function (resp) {
                    if (resp.data == "success") {
                        _this.dialogVisible2 = false;
                        // client_request，防止影响_this.selectAll()
                        _this.client_request = {};
                        // 重新查询所有数据
                        _this.selectAll();

                        // 修改数据成功提示消息
                        _this.$message({
                            message: '数据更新成功',
                            type: 'success'
                        });
                    }
                })
            },
            // 修改请求数据时点击取消
            cancelUpdateClientRequest() {
                this.dialogVisible2 = false;
                this.client_request = {};
                this.$message({
                    type: 'info',
                    message: '已取消修改'
            });
            },
            // 修改品牌数据时展示数据的方法
            selectById(array) {
                this.dialogVisible2 = true;
                // console.log(id);
                var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/myProject-plus/clientRequest/selectById?id=" + array.id
                }).then(function (resp) {
                    // 设置表单数据
                    _this.client_request = resp.data;
                })
            },
            // 查询分页数据
            selectAll() {
                /*var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/brand-case/brand/selectAll"
                }).then(function (resp) {
                    _this.tableData = resp.data;
                })*/

                var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/myProject-plus/clientRequest/selectByPageAndCondition?currentPage="+ _this.currentPage +"&pageSize=" + _this.pageSize,
                    data:_this.client_request
                }).then(function (resp) {
                    // 设置表格数据
                    _this.tableData = resp.data.rows;
                    // 设置总记录数
                    _this.totalCount = resp.data.totalCount;
                })
            },

            // 添加数据提示消息方法
            open() {
                this.$message({
                    message: '数据添加成功',
                    type: 'success'
                });
            },
            // 删除数据提示消息方法
            open2() {
                this.$message({
                    message: '数据删除成功',
                    type: 'success'
                });
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;

                console.log(this.multipleSelection)
            },
            // 点击查询的方法
            onSubmit() {
                this.selectAll();
            },
            // 添加数据提交表单方法
            addclientRequest() {
                var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/myProject-plus/clientRequest/add",
                    data: _this.client_request
                }).then(function (resp) {
                    if (resp.data == "success") {
                        // 添加成功
                        _this.dialogVisible = false;
                        _this.client_request = {};
                        _this.selectAll();
                        _this.open();
                    }
                })
            },
            // 添加数据时点击取消的方法
            cancelAddclientRequest() {
                this.dialogVisible = false;
                this.client_request = {};
            },
            // 分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                // 重新设置每页显示条数
                this.pageSize = val;
                this.selectAll();
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                // 重新设置当前页码
                this.currentPage = val;
                this.selectAll();
            },
            // 批量删除
            deleteByIds() {
                // 1.创建id数组，从this.multipleSelection获取即可
                for (let i = 0; i < this.multipleSelection.length; i ++) {
                    this.selectedIds[i] = this.multipleSelection[i].id;
                }

                // 弹出确认提示框
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 用户点击确认按钮
                    // 2.发送ajax请求
                    var _this = this;
                    axios({
                        method:"post",
                        url:"http://localhost:8080/myProject-plus/clientRequest/deleteByIds",
                        data: _this.selectedIds
                    }).then(function (resp) {
                        if (resp.data == "success") {
                            // 删除成功
                            _this.selectAll();
                            _this.open2();
                        }
                    })

                }).catch(() => {
                    // 用户点击取消按钮
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 删除单个数据
            deleteById(array){
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 点击确定删除
                    var _this = this;
                    axios({
                        method:"get",
                        url:"http://localhost:8080/myProject-plus/clientRequest/deleteById?id=" + array.id
                    }).then(function (resp) {
                        if (resp.data == "success") {
                            // 删除成功
                            _this.selectAll();
                            _this.open2();
                        }
                    })
                }).catch(() => {

                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        },
        data() {
            return {
                // 每页显示的条数
                pageSize:5,
                // 总记录数
                totalCount:100,
                // 当前页码
                currentPage: 1,
                // 添加数据的对话框是否展示的标记
                dialogVisible: false,
                // 修改数据的对话框是否展示的标记
                dialogVisible2:false,
                // 请求 模型数据
                client_request: {
                    id: '',
                    name:'',
                    requestType:'',
                    requestUrl:'',
                    requestHeaders:'',
                    bodyType:'',
                    body:''
                },
                // 表格数据
                tableData: [],
                // 复选框选中数据集合
                multipleSelection: [],
                // 被选中的id数组
                selectedIds:[]
            }
        }
    })
</script>

</body>
</html>